/**
 * Candidate number: 35834
 * April 8th 2012
 */
package tv.starfu.game;

import com.artemis.Entity;
import com.artemis.World;
import com.jme3.math.Vector3f;
import net.beadsproject.beads.core.AudioContext;
import net.beadsproject.beads.data.SampleManager;
import tv.starfu.components.AnimaSpatial;
import tv.starfu.components.FutureTransform;
import tv.starfu.components.level.Compass;
import tv.starfu.components.level.KyberneteGrid;
import tv.starfu.components.level.GridPosition;
import tv.starfu.components.level.Path;
import tv.starfu.components.ChaseCam;
import tv.starfu.components.MovementStyle;
import tv.starfu.components.CollisionMesh;
import tv.starfu.components.CollisionMesh.Shape;
import tv.starfu.components.InputControl;
import tv.starfu.components.RainbowWarrior;
import tv.starfu.components.SpaceshipControl;
import tv.starfu.components.Transform;
import tv.starfu.components.level.Cell;
import tv.starfu.sequencer.components.AmpEnvelope;
import tv.starfu.sequencer.components.PitchEnvelope;
import tv.starfu.sequencer.components.SamplePlayback;
import tv.starfu.sequencer.components.old.Channel;
import tv.starfu.sequencer.components.old.Event;
import tv.starfu.sequencer.components.SequenceComponent;
import tv.starfu.sequencer.components.old.SequenceEvent;
import tv.starfu.sequencer.components.TimeVector;

public class EntityFactory {

    //private KyberneteGrid kyberGrid;
    /**
     * new player entity for use in arcade mode
     * @param world
     * @param position  initial position
     * @param direction initial direction
     * @return player entity
     */
    public static Entity createArcadePlayer(Vector3f position, Vector3f direction, World world) {
        Entity player = world.createEntity();
        player.setGroup(EntityType.PLAYER.toString());
        player.setTag("PLAYER");
        
        player.addComponent(new AnimaSpatial("Player"));
        player.addComponent(new RainbowWarrior());
        player.addComponent(new Transform(position, direction));
        player.addComponent(new ChaseCam());
        player.addComponent(new FutureTransform());
        player.addComponent(new CollisionMesh(Shape.CAPSULE));
        player.addComponent(new MovementStyle());
        return player;
    }
    
    public static Entity createArcadeStarRacer(World world) {
        Entity player = world.createEntity();
        player.setGroup(EntityType.PLAYER.toString());
        player.setTag("PLAYER");
        
        player.addComponent(new AnimaSpatial("Player"));
        player.addComponent(new InputControl());
        player.addComponent(new SpaceshipControl());
        player.addComponent(new Transform());
        return player;
    }


    
    public static Entity constructKyberGrid(World world, KyberneteGrid kg) {
        Entity kyber = world.createEntity();
        kyber.setGroup(EntityType.TERRAIN.toString());
        kyber.setTag("TILEGRID");
        kyber.addComponent(kg);
        kyber.addComponent(new Transform());
        kyber.addComponent(new AnimaSpatial("TileGrid"));
        return kyber;
    }

    public static Entity createTile(World world, KyberneteGrid kg, int x, int y, Compass c) {
        Entity tile = world.createEntity();
        tile.setGroup(EntityType.TERRAIN.toString());        
        tile.setTag("TILE");
        tile.addComponent(new GridPosition(x,y));
        tile.addComponent(new Path(16));
        tile.addComponent(c);
        tile.addComponent(kg);
        tile.addComponent(new Cell());
        tile.addComponent(new AnimaSpatial("Tile"));
        tile.addComponent(new Transform());
        System.out.print("KyberGridSystem : just made a tile at grid[" + x + "][" + y + "]");
        return tile;
        
    }

    public static Entity createTerrain(World world) {
        Entity terrain = world.createEntity();
        terrain.setGroup(EntityType.TERRAIN.toString());        
        terrain.setTag("Terrain");
        terrain.addComponent(new AnimaSpatial("Terrain"));
        terrain.addComponent(new Transform());
        return terrain;
        
    }
    
    public static Entity createSequence(World world, int channelID, TimeVector now) {
        Entity sequence = world.createEntity();
        sequence.setGroup(EntityType.SEQUENCE.toString());
        sequence.setTag("sequence_a");
        //sequence.addComponent(new SequenceComponent());
        sequence.addComponent(new Channel(channelID));
        sequence.addComponent(now);
        return sequence;
    }
    
    public static Entity createEvent(World world, AudioContext ac, TimeVector tv) {
        Entity event = world.createEntity();
        event.setGroup(EntityType.SOUNDEVENT.toString());
        //event.setTag("drumEvent");
        event.addComponent(tv);
        event.addComponent(new SamplePlayback("kit1", 0));
        event.addComponent(new AmpEnvelope(new int[]{ 10, 400, 20, 10 }, new float[]{ 0f, 1f, 0.7f, 0.9f }, ac));
        return event;
    }
    
    /**
     * Create a new sequenced drum hit.
     * @param world
     * @param ac
     * @param tv
     * @param ampEnv
     * @param pitchEnv
     * @param sample
     * @return Entity drumhit
     */
    public static Entity createDrumHit(World world, AudioContext ac, 
                                        TimeVector tv, AmpEnvelope ampEnv, 
                                        PitchEnvelope pitchEnv, SamplePlayback sample) {
        
        Entity hit = world.createEntity();
        hit.addComponent(tv);
        hit.addComponent(ampEnv);
        hit.addComponent(pitchEnv);
        hit.addComponent(sample);
        hit.setGroup(EntityType.DRUMHIT.toString());
       // hit.setTag("drumHit");
        return hit;
    }
    
}
